import re
import locale


def unify_amount(amount_str) -> float:
    """
    统一金额字符串的格式。

    Args:
        amount_str (str): 金额字符串，可以包含逗号和小数点。

    Returns:
        float: 统一格式后的金额。

    """
    pattern = r'([\d,]*\.?\d+)(元|万元)'  # 正则表达式匹配金额字符串
    result = re.search(pattern, amount_str)
    if result:
        amount = float(result.group(1).replace(',', ''))
        unit = result.group(2)
        if unit == '万元':
            amount *= 10000
        return amount
    return None


def format_amount(amount) -> str:
    """
    格式化金额为带千位分隔符的字符串。

    Args:
        amount (float): 金额。

    Returns:
        str: 格式化后的金额字符串。

    """
    # 设置本地化环境为en_US.UTF-8
    locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
    # 格式化金额为带千位分隔符的字符串
    formatted_amount = locale.format_string('%.2f', amount, grouping=True)
    return formatted_amount


def get_chinese_amount(amount) -> str:
    """
    将金额转换为中文大写。

    Args:
        amount (float): 金额。

    Returns:
        str: 中文大写金额字符串。

    """
    numtext = '{:.2f}'.format(amount)  # 转换成2位小数
    decimal_part = int((amount - int(amount)) * 100)
    digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
    units = [
        '分', '角', '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟',
        '万'
    ]
    temptext = '零'
    tempid = 0
    for i in numtext[::-1]:
        if i != '.':
            if int(i) == 0:
                if units[tempid] in ['元', '万', '亿']:
                    temptext = units[tempid] + temptext
                elif temptext[0] not in ['零', '元', '万', '亿']:
                    temptext = '零' + temptext
            else:
                temptext = digits[int(i)] + units[tempid] + temptext
            tempid += 1
    temptext = temptext[:-1]
    if '亿万' in temptext:
        temptext = temptext.replace('亿万', '亿')

    if decimal_part == 0:
        temptext += '整'
    return temptext

# 示例用法
if __name__ == '__main__':
    amount = 8920006000100.56
    chinese_amount_result = get_chinese_amount(amount)
    print(chinese_amount_result)

    a = '860,000.58元'
    b = '760450万元'
    c = '100000元'

    amount_a = unify_amount(a)
    amount_b = unify_amount(b)
    amount_c = unify_amount(c)

    if amount_a is not None:
        formatted_amount_a = format_amount(amount_a)
        chinese_amount_a = get_chinese_amount(amount_a)
        print(formatted_amount_a, '元')
        print(chinese_amount_a)
    else:
        print('无法提取金额')

    if amount_b is not None:
        formatted_amount_b = format_amount(amount_b)
        chinese_amount_b = get_chinese_amount(amount_b)
        print(formatted_amount_b, '元')
        print(chinese_amount_b)
    else:
        print('无法提取金额')

    if amount_c is not None:
        formatted_amount_c = format_amount(amount_c)
        chinese_amount_c = get_chinese_amount(amount_c)
        print(formatted_amount_c, '元')
        print(chinese_amount_c)
    else:
        print('无法提取金额')
